package com.wewins.fota.mapper;

import com.wewins.fota.bean.bo.filter.FilterCond;
import com.wewins.fota.bean.po.UpdatePlan;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UpdatePlanMapper {
    @Select({
            "<script>",
            "SELECT * FROM update_plan WHERE 1=1 ",
            "<if test=\"name != null and name != ''\" >",
            "AND locate(#{name}, name) > 0",
            "</if>",
            "<if test=\"type != null and type != ''\" >",
            "AND locate(#{type}, cond) > 0",
            "</if>",
            "<if test=\"owner != null and owner != ''\" >",
            "AND locate(#{owner}, cond) > 0",
            "</if>",
            "<if test=\"product != null and product != ''\" >",
            "AND product=#{product}",
            "</if>",
            "<if test=\"srcV != null and srcV != ''\" >",
            "AND locate(#{srcV}, src_v) > 0",
            "</if>",
            "<if test=\"dstV != null and dstV != ''\" >",
            "AND dst_v=#{dstV}",
            "</if>",
            "<if test='minTime != null and minTime != 0' >",
            "<![CDATA[ AND create_time>=#{minTime} ]]>",
            "</if>",
            "<if test='maxTime != null and maxTime != 0' >",
            "<![CDATA[ AND create_time<=#{maxTime} ]]>",
            "</if>",
            "</script>"
    })
    @Results(id = "queryMap", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "srcV", column = "src_v"),
            @Result(property = "dstV", column = "dst_v"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time"),
    })
    List<UpdatePlan> queryAll(FilterCond cond);

    @Select("SELECT * FROM update_plan WHERE id = #{id}")
    @ResultMap(value = "queryMap")
    UpdatePlan findById(int id);

    @Insert("INSERT INTO update_plan (name, product, cond, src_v, dst_v, create_time, update_time, description, status) VALUES (#{name}, #{product}, #{cond}, #{srcV}, #{dstV}, #{createTime}, #{updateTime}, #{description}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(UpdatePlan data);

    @Delete("DELETE FROM update_plan WHERE id = #{id}")
    int delete(UpdatePlan plan);

    int update(UpdatePlan data);

    @Select("SELECT * FROM update_plan where product = #{product} AND locate(#{version}, src_v) > 0")
    @ResultMap(value = "queryMap")
    List<UpdatePlan> queryByProductAndSrcV(String product, String version);
}
